﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_ManageGroups : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Roles.IsUserInRole(Membership.GetUser().UserName, "Admin") && !Roles.IsUserInRole(Membership.GetUser().UserName, "Instructor"))
                Response.Redirect("~/");
        }
        catch
        {
            Response.Redirect("~/");
        }

        if (!IsPostBack)
        {
            BindGroups();
            BindCourses();
        }
    }

    private void BindGroups()
    {
        grdGroups.DataSource = LMSData.GroupController.GetGroups();
        grdGroups.DataBind();
    }

    private void BindCourses()
    {
        cblAssignCourses.DataSource = LMSData.CourseController.GetCourses();
        cblAssignCourses.DataBind();
    }

    protected void LoadCourses(object sender, EventArgs e)
    {
        string gid = ((HiddenField)((Panel)sender).FindControl("hidGroupID")).Value;
        foreach (LMSData.Course c in LMSData.GroupController.GetCourses(gid))
            ((Panel)sender).Controls.Add(new LiteralControl(c.CourseName + "<br>"));
    }

    protected void OpenEdit(object sender, CommandEventArgs e)
    {
        LMSData.Group grp = new LMSData.Group(e.CommandArgument.ToString());
        txtEditGroupName.Text = grp.GroupName;
        txtEditDescription.Text = grp.Description;
        cbEditPublic.Checked = grp.IsPublic;

        cblEditAssignCourses.DataSource = LMSData.CourseController.GetCourses();
        cblEditAssignCourses.DataBind();

        LMSData.CourseCollection cc = LMSData.GroupController.GetCourses(e.CommandArgument.ToString());

        foreach (ListItem li in cblEditAssignCourses.Items)
        {
            li.Selected = false;
            foreach(LMSData.Course crs in cc)
            {
                if (li.Value == crs.CourseID.ToString())
                {
                    li.Selected = true;
                    break;
                }
            }
        }
        hidSelectedGroupID.Value = e.CommandArgument.ToString();
        UpdatePanelPopupExtender2.ShowAt((LinkButton)sender);
    }

    protected void AddGroup(object sender, EventArgs e)
    {
        LMSData.Group grp = new LMSData.Group();
        grp.GroupName = txtGroupName.Text;
        grp.Description = txtDescription.Text;
        grp.IsPublic = cbPublic.Checked;
        grp.Save();

        foreach (ListItem li in cblAssignCourses.Items)
        {
            LMSData.CourseGroupMap cgm = new LMSData.CourseGroupMap();
            if (li.Selected)
            {
                cgm.CourseID = Int32.Parse(li.Value);
                cgm.GroupID = grp.GroupID;
                cgm.Save();
            }
        }
        BindGroups();
        update1.Update();
    }

    protected void EditGroup(object sender, EventArgs e)
    {
        LMSData.Group grp = new LMSData.Group(hidSelectedGroupID.Value);
        grp.GroupName = txtEditGroupName.Text;
        grp.Description = txtEditDescription.Text;
        grp.IsPublic = cbEditPublic.Checked;
        grp.Save();

        LMSData.CourseGroupMap.Delete(LMSData.CourseGroupMap.Columns.GroupID, grp.GroupID);

        foreach (ListItem li in cblEditAssignCourses.Items)
        {
            LMSData.CourseGroupMap cgm = new LMSData.CourseGroupMap();
            if (li.Selected)
            {
                cgm.CourseID = Int32.Parse(li.Value);
                cgm.GroupID = grp.GroupID;
                cgm.Save();
            }
        }
        BindGroups();
        UpdatePanelPopupExtender2.Hide();
        update1.Update();
    }

    protected void CancelEditGroup(object sender, EventArgs e)
    {
        UpdatePanelPopupExtender2.Hide();
    }

    protected void UpdatePanelPopupExtender2_Close(object sender, EventArgs e)
    {
    }

    protected void DeleteGroup(object sender, CommandEventArgs e)
    {
        LMSData.Group grp = new LMSData.Group(e.CommandArgument.ToString());
        grp.IsDeleted = true;
        grp.Save();
        BindGroups();
        update1.Update();
    }

    protected void CheckPublic(object sender, EventArgs e)
    {
        if (((Label)sender).Text == "True")
            ((Label)sender).Text = char.ConvertFromUtf32(10004);
        else
            ((Label)sender).Text = "";
    }
}
